﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XiaoYuJi.Core.Extends;
using XiaoYuJi.Entity;
using XiaoYuJi.Entity.BusEntitys;
using XiaoYuJi.Repository.IRepository.IBusRepository;
using XiaoYuJi.Service.IServices.IBusService;

namespace XiaoYuJi.Service.BusServices;
public class ArticleService : BaseService<Article>, IArticleService
{
    public ArticleService(IArticleRepository articleRepository)
        : base(articleRepository)
    {

    }
    /// <summary>
    /// 获取文章列表列表数据分页
    /// </summary>
    /// <param name="title">标题</param>
    /// <param name="content">内容</param>
    /// <param name="classifyId">分类</param>
    /// <param name="pageIndex">当前页</param>
    /// <param name="pageSize">每页显示行</param>
    /// <returns></returns>
    public async Task<ApiResult<PageData<Article>>> GetArticleListPageAsync(string title, string content
        , int? classifyId, int pageIndex = 1, int pageSize = 10)
    {
        //构造条件
        IQueryable<Article> query = base._repository.Query()
            .WhereIf(e => e.Title.Contains(title), !string.IsNullOrEmpty(title))
            .WhereIf(e => e.Content.Contains(content), !string.IsNullOrEmpty(content))
            .WhereIf(e => e.ClassifyId == classifyId, classifyId != null);

        //获取文章数量
        int count = _repository.Count();

        //获取文章分页数据
        List<Article> articles = await query.Skip((pageIndex - 1) * pageSize)   //1，10，1-1=0，结果=0
                    .Take(pageSize)
                    .ToListAsync();

        #region 不使用WhereIf扩展方法的写法
        /*
         IQueryable<Article> queryable = base._repository.Query();
         if (!string.IsNullOrEmpty(title))
             queryable = queryable.Where(e => e.Title.Contains(title));
         if(classifyId != null)
             queryable = queryable.Where(e => e.ClassifyId == classifyId);*/
        #endregion

        //构造分页返回类
        PageData<Article> pageData = new()
        {
            PageIndex = pageIndex,
            PageSize = pageSize,
            List = articles,
            Total = count
        };

        ApiResult<PageData<Article>> apiResult = new()
        {
            Data = pageData,
            Message = "获取分页文章数据列表！"
        };

        return apiResult;
    }

    public async Task<ApiResult<PageData<Article>>> GetArticleListPageAsync(string keyword, int? classifyId
        , int pageIndex = 1, int pageSize = 10)
    {
        //获取分页文章数据列表、数据总数
        List<Article> articleList = await base.Query(e => !e.IsDelete)
             .WhereIf(e => e.Title.Contains(keyword)
                 || e.Content.Contains(keyword), !string.IsNullOrEmpty(keyword))
             .WhereIf(e => e.ClassifyId == classifyId, classifyId != null)
             .Paging(pageIndex, pageSize, out int count)   //合理使用扩展方法
             .ToListAsync();

        //构造一个分页返回数据
        PageData<Article> pageData = new()
        {
            Total = count,
            List = articleList,
            PageIndex = pageIndex,
            PageSize = pageSize
        };

        ApiResult<PageData<Article>> apiResult = new()
        {
            Data = pageData,
            Message = "获取分页文章数据列表~"
        };

        return apiResult;
    }


}
